import socket

# 创建TCP套接字
server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

# 绑定地址和端口
server_address = ('localhost', 8888)
server_socket.bind(server_address)

# 开始监听，最大连接数为5
server_socket.listen(5)
print(f"服务器正在监听 {server_address[0]}:{server_address[1]}...")

# 接受客户端连接
client_socket, client_address = server_socket.accept()
print(f"接受来自 {client_address} 的连接")

try:
    while True:
        # 接收客户端消息通信.py
        data = client_socket.recv(1024)
        if not data:
            break
        print(f"客户端说: {data.decode('utf-8')}")

        # 发送响应给客户端
        response = input("服务端回应: ")
        client_socket.send(response.encode('utf-8'))

finally:
    # 关闭连接
    client_socket.close()
    server_socket.close()